package controllers;

import play.*;
import play.mvc.*;

import java.util.*;

import models.*;

@With(Secure.class)
// this class is used for the login, Doaa El-Hawary
public class Plans extends Controller {

	public static void index() {
		render();
	}

	/**
	 * Adds Requirements & status to a plan
	 * 
	 * @author Youssef Selim
	 * @version 2011.0425
	 * @category C5S6
	 * @param requirements
	 *            : The requirements for the plan, status: the status of the
	 *            plan planID: The id of the plan
	 */
	public static void addReqAndStatus(String requirements, boolean status,
			long planID) {
		Plan plan = Plan.findById(planID);
		if (plan == null)
			error("The plan doesn't exist !");

		User u = User.findByUsername(Security.connected());
		if (!plan.getUsers().contains(u))
			error("You don't have the permission for this action !");

		LinkedList<String> requirementsList = new LinkedList<String>();

		String[] requirementsString = requirements.split(",");

		for (int i = 0; i < requirementsString.length; i++) {
			requirementsList.add(requirementsString[i]);
		}

		plan.setRequirements(requirementsList);
		plan.setStatus(status);
		plan.save();
		redirect("/application/success");
	}

	/**
	 * Views Requirements & status of a plan
	 * 
	 * @author Youssef Selim
	 * @version 2011.0425
	 * @category C5S7
	 * @param planID
	 *            : The id of the plan to get the requirements and status
	 */
	public static void viewReqAndStatus(long planID) {
		Plan plan = Plan.findById(planID);
		if (plan == null)
			error("The plan doesn't exist !");
		LinkedList<String> requirements = plan.getRequirements();

		String str = "";
		for (int i = 0; i < requirements.size(); i++) {
			if (i == 0)
				str = requirements.get(i);

			else
				str = str + "," + requirements.get(i);

			if (requirements.size() == 1)
				str = requirements.get(i);

		}

		Boolean status = plan.isStatus();
		render(str, status, planID);
	}

	/**
	 * Views timline of a certain plan
	 * 
	 * @author Youssef Selim
	 * @version 2011.0504
	 * @category C5S11
	 * @param planID
	 *            : The id of the plan to get it's timeline
	 */
	public static void viewTimeline(long planID) {
		Plan plan = Plan.findById(planID);
		if (plan == null)
			error("The plan doesn't exist !");
		LinkedList<String> timeline = plan.getTimeline();
		render(timeline);
	}

	/**
	 * Previews html view form to fill in requirements and status
	 * 
	 * @author Youssef Selim
	 * @version 2011.0425
	 * @category C5S6
	 * @param organizationID
	 *            : The id of the organization to get it's plans
	 */
	public static void fillAddReqAndStatusForm(long organizationID) {
		Organization organization = Organization.findById(organizationID);
		if (organization == null)
			error("The organization doesn't exist !");
		List plans = organization.getPlans();
		render(plans);
	}

	/**
	 * Adds an Idea to be associated the Plan while checking that the idea is
	 * already not associated
	 * 
	 * @author Andrew Guirguis
	 * @version 2011.0425
	 * @category C5S2
	 * @params ideaID: The id of idea to be associated planID: The id of the
	 *         plan
	 * 
	 * 
	 */

	public static void addIdea(Long ideaID, Long planID) {
		Idea item = Idea.findById(ideaID);
		Plan plan = Plan.findById(planID);
		String itemName = item.getTitle();
		String planName = plan.getName();

		String userName = session.get("username");
		User user = (User) User.find("byUsername", userName).fetch().get(0);

		if (!plan.getUsers().contains(user) && !user.isAdmin()) {

			error("You are not autorized to do this action");
		}

		if (!item.isExecutionStatus()) {
			error("The following idea" + itemName
					+ " is not promoted to execution");
			return;
		}

		if (plan.getIdeas().contains(item)) {
			error("The following idea" + itemName
					+ " is already associated with this plan " + planName);
			return;
		}
		if (item.getPlan() != null) {
			error("The following idea" + itemName
					+ " is already associated with another plan " + planName);
			return;
		}
		plan.getIdeas().add(item);
		plan.save();
		item.setPlan(plan);
		item.save();

		addIdeaView("The following plan " + planName
				+ " was successfully associated with the Idea " + itemName);

	}

	/**
	 * selecting the idea to be associated with a plan from the rendered list of
	 * ideas sent to addIdeaView.html
	 * 
	 * 
	 * @author Andrew Guirguis
	 * @version 2011.0503
	 * @category C5S2
	 * @params result: The result passed from addIdea() whether there is success
	 *         in associating a certain plan with a certain idea
	 * 
	 */

	public static void addIdeaView(String result) {
		// new Idea().save(); //just for testing
		List<Idea> ideas = Idea.findAll();
		if (ideas.size() == 0)
			error("No Ideas stored in database");
		List<Plan> plans = Plan.findAll();
		if (plans.size() == 0)
			error("No Plans stored in database");

		render(ideas, plans, result);

	}

	/**
	 * creating the calendar of items included in the plan
	 * 
	 * 
	 * @author Amir Rached
	 * @version 2011.0503
	 * @category C5S12
	 * 
	 * 
	 */
	public static void createCalendar(int id, long planId) {

		List<Item> PlanItems1 = Item.findAll();
		LinkedList<Item> PlanItems = new LinkedList<Item>();
		for (int i = 0; i < PlanItems1.size(); i++) {
			if (PlanItems1.get(i).getPlan().getId() == planId) {
				PlanItems.add(PlanItems1.get(i));
			}
		}

		ArrayList<LinkedList<Item>> AllItems = new ArrayList<LinkedList<Item>>();

		int k = -1;

		do {
			k++;
			LinkedList<Item> e = new LinkedList<Item>();
			AllItems.add(e);

		} while (AllItems.size() < 120);

		for (int i = 0; i < PlanItems.size(); i++) {
			for (int j = 0; j < 120; j++) {
				if (AllItems.get(j).size() > 0) {
					if (AllItems.get(j).get(0) != null) {
						if (AllItems.get(j).get(0).getDateAndTime().getYear() == PlanItems
								.get(i).getDateAndTime().getYear()
								&& AllItems.get(j).get(0).getDateAndTime()
										.getMonth() == PlanItems.get(i)
										.getDateAndTime().getMonth()) {
							AllItems.get(j).add(PlanItems.get(i));
							break;
						}
					}
				}
			}
			for (int j = 0; j < 120; j++) {
				if (AllItems.get(j).size() == 0) {
					LinkedList<Item> t = new LinkedList<Item>();
					AllItems.add(j, t);
					AllItems.get(j).add(PlanItems.get(i));
					break;
				}
			}
		}

		ArrayList<LinkedList<Item>> ItemsDate = new ArrayList<LinkedList<Item>>(
				32);

		k = -1;

		do {
			k++;
			LinkedList<Item> e = new LinkedList<Item>();
			ItemsDate.add(e);

		} while (ItemsDate.size() < 32);
		String Title = "";
		for (int i = 0; i < ItemsDate.size(); i++) {
			for (int j = 0; j < AllItems.get(id).size(); j++) {
				if (AllItems.get(id).get(j).getDateAndTime().getDate() == i) {
					ItemsDate.get(i).add(AllItems.get(id).get(j));

				}
			}
		}
		/*
		 * switch(AllItems.get(id).get(0).getDateAndTime().getMonth()) { case
		 * 1:Title = "January , ";break; case 2:Title = "February , ";break;
		 * case 3:Title = "March , ";break; case 4:Title = "April , ";break;
		 * case 5:Title = "May , ";break; case 6:Title = "June , ";break; case
		 * 7:Title = "July , ";break; case 8:Title = "August , ";break; case
		 * 9:Title = "September , ";break; case 10:Title = "October , ";break;
		 * case 11:Title = "November , ";break; case 12:Title =
		 * "December , ";break; default: Title = "eshta";
		 * 
		 * } Title = Title+AllItems.get(id).get(0).getDateAndTime().getYear();
		 */
		int NId = id + 1;
		int PId = id - 1;
		if (PId < 0) {
			PId = 0;

		} else {
			if (NId == AllItems.size()) {
				NId = AllItems.size() - 1;

			}
		}

		render(ItemsDate, NId, PId, Title, planId);

	}

	/**
	 * views all the plans
	 * 
	 * @author Arwa Ismail
	 * @version 2010.0505
	 * @category C4S8
	 */

	public static void viewAllPlans() {
		List<Plan> plans = Plan.findAll();
		render(plans);
	}

	/**
	 * views a certain plan
	 * 
	 * @author Arwa Ismail
	 * @version 2010.0505
	 * @category C4S8
	 * @ModifiedBy Youssef Selim
	 * @Added Rendering a flag to check whether the user viewing the plan is
	 *        with organizer privileges or not
	 */
	public static void viewPlan(long planID) {

		Plan plan = Plan.findById(planID);
		List<Item> items = plan.getItems();

		/** Modification part Author: Youssef Selim **/
		User u = User.findByUsername(Security.connected());
		boolean organizerFlag = false;
		if (plan.getUsers().contains(u))
			organizerFlag = true;

		render(items, plan, organizerFlag);
		/***************************************************/
	}
}